import request from '../request'
import type { ApiResponse, PaginationParams, PaginationResponse } from '../types'

export interface Product {
  id: number
  name: string
  price: number
  description: string
  images: string[]
  stock: number
  category: string
}

export interface ProductDetail extends Product {
  specifications: Record<string, string>
  reviews: Review[]
}

export interface Review {
  id: number
  userId: number
  username: string
  rating: number
  content: string
  createdAt: string
}

// 获取商品列表 - 游客可访问，有token时带上
export const getProducts = (params: PaginationParams & { category?: string }) => {
  return request.get<ApiResponse<PaginationResponse<Product>>>('/dev/products', { params })
}

// 获取商品详情 - 游客可访问，有token时带上
export const getProductDetail = (id: number) => {
  return request.get<ApiResponse<ProductDetail>>(`/dev/products/${id}`)
}

// 添加商品到购物车 - 需要登录
export const addToCart = (productId: number, quantity: number) => {
  return request.post<ApiResponse>('/dev/cart', { productId, quantity })
}

// 获取购物车列表 - 需要登录
export const getCartList = () => {
  return request.get<ApiResponse>('/dev/cart')
}

// 提交订单 - 需要登录
export const createOrder = (data: {
  items: Array<{ productId: number; quantity: number }>
  addressId: number
}) => {
  return request.post<ApiResponse>('/dev/orders', data)
} 